

// 轮播图数据
swiper()
function swiper() {
    $.ajax({
        type: 'get',
        url: 'http://localhost:8081/home/swiper',
        success(mes) {
            let data = mes.body
            data.forEach(item => {
                let str = `
                 <div class="swiper-slide">
                    <img src="http://localhost:8081${item.imgSrc}" alt="">
                </div>
                 `
                $('.spp').append(str)
            });
            var mySwiper = new Swiper('.swiper-container', {
                autoplay: 2000,//可选选项，自动滑动
            });
        }
    })
}

// 登录/注册+个人信息
logins()
function logins() {
    let token = localStorage.getItem('token')
    if (token) {
        $('.login').css({
            display: 'none'
        })
        $.ajax({
            type: "get",
            url: "http://localhost:8081/user",
            headers: {
                'authorization': token
            },
            success: function (response) {
                let data = response.body
                let module = `
                <li class="sign" style="line-height: 35px;"><a href="./3.15login.sign/login.html"><img src="http://localhost:8081${data.avatar}" alt=""></a><a href="./public/information.html"><span>${data.nickname}</span></a></li>
            `
                $('.navfooter').append(module)
                $('.sign').css({
                    display: 'block'
                })
            }
        });
    } else {
        $('.login').css({
            display: 'block'
        })
    }
}


// 二手房源数据
resources()
function resources() {
    // 获取选中地区ID
    let id = localStorage.getItem('id')
    // 获取城市定位信息
    let city = JSON.parse(localStorage.getItem('data'))
    $.ajax({
        type: "get",
        url: "http://localhost:8081/houses?city",
        data: {
            cityId: id
        },
        success: function (response) {
            let data = response.body.list
            localStorage.setItem('cx', JSON.stringify(data))
            // 渲染房屋信息
            data.forEach(item => {
                let module = `
                    <div class="col-lg-3 col-md-4 col-xs-6 col-sm-6 col">
                            <div class="list">
                            <div class="top_img"><img src="http://localhost:8081${item.houseImg ? item.houseImg : './public/img/1.jpg'}" alt=""></div>
                            <div class="list_title">
                                <h3>${item.title}</h3>
                                <p>${item.desc}</p>
                            </div>
                        </div>
                    </div>
                `
                $('.row_').append(module)
            })
            // 城市选中
            if (city){
                city.forEach(function (item) {
                    if (item.value == id) {
                        let module1 = `<a href="./public/city.html">${item.label}</a>`
                        $('.fo').append(module1)
                    }
                })
            }else {
                let module1 = `<a href="./public/city.html">选择地区</a>`
                $('.fo').append(module1)
            }
        }
    });
}


// 优选数据
y()
function y() {
    let id = localStorage.getItem('id')
    $.ajax({
        type: "get",
        url: "http://localhost:8081/home/news?area",
        data: {
            area: id
        },
        success: function (response) {
            let data = response.body
            data.forEach(item => {
                let module =
                    `
                     <div class="col-md-3 col-xs-6 col-sm-6 col_">
                <div class="list">
                    <!-- 图片 -->
                    <div class="top_img"><img src="http://localhost:8081${item.imgSrc}" alt=""></div>
                    <!-- 介绍 -->
                    <div class="list_title">
                        <h3>${item.title}</h3>
                        <p>${item.from}</p>
                    </div>
                </div>
            </div>
                `
                $('.row_one').append(module)
            })
        }
    });
}